package system;

import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class SheetData {

    private  XSSFWorkbook wb;
    private FileInputStream fileInputStream;
    private String filePath;
    private boolean isFirst=true;


    public SheetData(String filePath){
        this.filePath=filePath;
        File file=new File(filePath);
        if(!this.isFirst&&file.exists()){
            try {
                fileInputStream=new FileInputStream(file);
                this.wb=new XSSFWorkbook(fileInputStream);
            } catch (IOException e) {
                e.printStackTrace();
                this.wb=new XSSFWorkbook();
            }
        }else{
            wb=new XSSFWorkbook();
            this.isFirst=false;
        }
    }

    public List<Map<String,Object>> creatSheet(List<Map<String,Object>> datas,int rowCount,int colCount,String SheetName,String sheetTitle){

        try{
            XSSFSheet sheet = wb.createSheet(SheetName);
            CellRangeAddress range=new CellRangeAddress(0,0,0,16);
            sheet.addMergedRegion(range);
            XSSFRow row=sheet.createRow(0);
            row.setHeight((short) (39.75*20));
            XSSFCell cell=row.createCell(0);
            cell.setCellValue(sheetTitle);
            XSSFCellStyle cellStyle=wb.createCellStyle();
            //垂直居中
            cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
            //水平居中
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            Font font = wb.createFont();
            font.setBold(true);
            font.setFontHeightInPoints((short) 20);
            font.setFontName("宋体");
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);

            List<Map<String,Object>> result=creat(datas,rowCount,colCount,wb,sheet);

            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
//            wb.close();

            return result;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }

    private List<Map<String,Object>> creat(List<Map<String,Object>> datas,int rowCount, int colCount, XSSFWorkbook wb, XSSFSheet sheet){
        int index=0;
        List<Map<String,Object>> result=new ArrayList<>();
        for(int r=0;r<rowCount;r++){
            XSSFRow row=sheet.createRow(r+1);
            row.setHeight((short) (67.25*20));
            for(int c=0;c<colCount;c++){
                index=c*rowCount+r;
                XSSFCell cell=row.createCell(c*3);
                if(datas!=null&&datas.size()>index){
                    datas.get(index).put("考场",sheet.getSheetName());
                    datas.get(index).put("座位号",index+1);
                    result.add(datas.get(index));
                    cell.setCellValue((String)datas.get(index).get("考生号"));
                }else{
                    cell.setCellValue("");
                }
                XSSFCellStyle cellStyle = wb.createCellStyle();
                cellStyle.setBorderTop(BorderStyle.THIN);//上边框
                cellStyle.setBorderBottom(BorderStyle.THIN); //下边框
                cellStyle.setBorderLeft(BorderStyle.THIN);//左边框
                cellStyle.setBorderRight(BorderStyle.THIN);//右边框
                cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
                Font font = wb.createFont();
                font.setBold(true);
                font.setFontHeightInPoints((short) 11);
                font.setFontName("Tahoma");
                cellStyle.setFont(font);
                cell.setCellStyle(cellStyle);

                XSSFCell cell1=row.createCell(c*3+1);
                if(datas!=null&&datas.size()>index){
                    cell1.setCellValue((String)datas.get(index).get("姓名"));

                }else{
                    cell1.setCellValue("");
                }
                XSSFCellStyle cellStyle1 = wb.createCellStyle();
                cellStyle1.setBorderTop(BorderStyle.THIN);//上边框
                cellStyle1.setBorderBottom(BorderStyle.THIN); //下边框
                cellStyle1.setBorderLeft(BorderStyle.THIN);//左边框
                cellStyle1.setBorderRight(BorderStyle.THIN);//右边框
                cellStyle1.setWrapText(true);
                cellStyle1.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
                cellStyle1.setAlignment(HorizontalAlignment.CENTER);
                Font font1 = wb.createFont();
                font1.setBold(true);
                font1.setFontHeightInPoints((short) 11);
                font1.setFontName("宋体");
                cellStyle1.setFont(font1);
                cell1.setCellStyle(cellStyle1);

                row.createCell(c*3+2);

            }
        }
        for(int c=0;c<colCount-1;c++){
            CellRangeAddress range=new CellRangeAddress(1,rowCount,c*3+2,c*3+2);
            sheet.addMergedRegion(range);
            XSSFCell cell=sheet.getRow(1).getCell(c*3+2);
            XSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.THIN);//上边框
            cell.setCellStyle(cellStyle);
            cell=sheet.getRow(rowCount).getCell(c*3+2);
            cellStyle = wb.createCellStyle();
            cellStyle.setBorderBottom(BorderStyle.THIN);//上边框
            cell.setCellStyle(cellStyle);
        }
        for(int c=0;c<colCount;c++){
            sheet.setColumnWidth(c*3,14*256);
            sheet.setColumnWidth(c*3+1,2*256*2);
            if(c<6-1){
                sheet.setColumnWidth(c*3+2,(int)(1.5*256*2));
            }
        }
        sheet.createRow(rowCount+1);
        sheet.createRow(rowCount+2);
        sheet.createRow(rowCount+3);

        for(int i=0;i<4;i++){
            XSSFCell cell=sheet.getRow(rowCount+2).createCell((int)(Math.ceil(colCount*3/2)-2-1+i));
            XSSFCellStyle cellStyle = wb.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
            cellStyle.setBorderBottom(BorderStyle.MEDIUM);
            cellStyle.setBorderLeft(BorderStyle.MEDIUM);
            cellStyle.setBorderRight(BorderStyle.MEDIUM);
            cell.setCellStyle(cellStyle);
            cell=sheet.getRow(rowCount+3).createCell((int)(Math.ceil(colCount*3/2)-2-1+i));
            cellStyle = wb.createCellStyle();
            cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
            cellStyle.setBorderBottom(BorderStyle.MEDIUM);
            cellStyle.setBorderLeft(BorderStyle.MEDIUM);
            cellStyle.setBorderRight(BorderStyle.MEDIUM);
            cell.setCellStyle(cellStyle);
        }
        CellRangeAddress range=new CellRangeAddress(rowCount+2,rowCount+3,(int)(Math.ceil(colCount*3/2)-1-2),(int)(Math.ceil(colCount*3/2)-1+1));
        sheet.addMergedRegion(range);
        XSSFCell cell=sheet.getRow(rowCount+2).getCell((int)(Math.ceil(colCount*3/2)-2-1));
        cell.setCellValue("讲  台");
        Font font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 16);
        font.setFontName("宋体");
        cell.getCellStyle().setFont(font);
        cell.getCellStyle().setVerticalAlignment(cell.getCellStyle().getVerticalAlignmentEnum().CENTER);
        cell.getCellStyle().setAlignment(HorizontalAlignment.CENTER);

        String sheetName=sheet.getSheetName();
        if(getDoor(Integer.parseInt(sheetName.substring(sheetName.indexOf("第")+1,sheetName.indexOf("考场")))).equals("左")){
//            range=new CellRangeAddress(rowCount+2,rowCount+2,0,1);
            cell=sheet.getRow(rowCount+2).createCell(0);
        }else{
//            range=new CellRangeAddress(rowCount+2,rowCount+2,colCount*3-2-1,colCount*3-2);
            cell=sheet.getRow(rowCount+2).createCell(colCount*3-2-1);
        }
        XSSFCellStyle cellStyle = wb.createCellStyle();
        cell.setCellValue("正 门");
//        sheet.addMergedRegion(range);
        font = wb.createFont();
        font.setBold(true);
//        font.setFontHeightInPoints((short) 16);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        cellStyle.setVerticalAlignment(cell.getCellStyle().getVerticalAlignmentEnum().CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setBorderTop(BorderStyle.MEDIUM);//上边框
        cellStyle.setBorderBottom(BorderStyle.MEDIUM);
        cellStyle.setBorderLeft(BorderStyle.MEDIUM);
        cellStyle.setBorderRight(BorderStyle.MEDIUM);
        cell.setCellStyle(cellStyle);

        XSSFRow row=sheet.createRow(rowCount+4+1);
        for(int i=0;i<colCount*3-1;i++){
            row.createCell(i);
        }
        range=new CellRangeAddress(rowCount+4+1,rowCount+4+1,0,colCount*3-1);
        sheet.addMergedRegion(range);
        cell=row.getCell(0);
        cellStyle = wb.createCellStyle();
        cell.setCellValue(sheetName+" 高二("+Integer.parseInt(sheetName.substring(sheetName.indexOf("第")+1,sheetName.indexOf("考场")))+")班 "+rowCount*colCount+"人");
        font = wb.createFont();
        font.setBold(true);
        font.setFontHeightInPoints((short) 20);
        font.setFontName("宋体");
        cellStyle.setFont(font);
        cellStyle.setVerticalAlignment(cell.getCellStyle().getVerticalAlignmentEnum().CENTER);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cell.setCellStyle(cellStyle);


        System.out.println("数据量:"+datas.size());
        datas.removeAll(result);
        return result;
    }

    public void createResultSheet(List<Map<String,Object>> datas,String sheetName,String sheetTitle){
        try{
            XSSFSheet sheet = wb.createSheet(sheetName);
            CellRangeAddress range=new CellRangeAddress(0,0,0,4);
            sheet.addMergedRegion(range);
            XSSFRow row=sheet.createRow(0);
            row.setHeight((short) (39.75*20));
            XSSFCell cell=row.createCell(0);
            cell.setCellValue(sheetTitle);
            XSSFCellStyle cellStyle=wb.createCellStyle();
            //垂直居中
            cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
            //水平居中
            cellStyle.setAlignment(HorizontalAlignment.CENTER);
            Font font = wb.createFont();
            font.setBold(true);
            font.setFontHeightInPoints((short) 20);
            font.setFontName("宋体");
            cellStyle.setFont(font);
            cell.setCellStyle(cellStyle);

            row=sheet.createRow(1);
            String[] names=new String[]{"高二学号","考生号","姓名","考场","座位号"};
            for(int i=0;i<5;i++){
                cell=row.createCell(i);
                cell.setCellValue(names[i]);
                cellStyle=wb.createCellStyle();
                //垂直居中
                cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
                //水平居中
                cellStyle.setAlignment(HorizontalAlignment.CENTER);
                font = wb.createFont();
                font.setBold(true);
//                font.setFontHeightInPoints((short) 20);
                font.setFontName("宋体");
                cellStyle.setFont(font);
                cell.setCellStyle(cellStyle);

                sheet.setColumnWidth(i,13*256);
            }

            Iterator<Map<String,Object>> iterator=datas.iterator();
            int index=2;
            while(iterator.hasNext()){
                Map<String,Object> data=iterator.next();
                row=sheet.createRow(index);
                index++;
                for(int i=0;i<5;i++){
                    cell=row.createCell(i);
//                    System.out.println(data);
                    cell.setCellValue(data.get(names[i]).toString());
                    cellStyle=wb.createCellStyle();
                    //垂直居中
                    cellStyle.setVerticalAlignment(cellStyle.getVerticalAlignmentEnum().CENTER);
                    //水平居中
                    cellStyle.setAlignment(HorizontalAlignment.CENTER);
                    cell.setCellStyle(cellStyle);
                }
            }

            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            wb.write(fileOutputStream);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void closeInput(){
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getDoor(int classNumber){
        switch (classNumber){
            case 1:return "左";
            case 2:return "右";
            case 3:return "右";
            case 4:return "左";
            case 5:return "右";
            case 6:return "左";
            case 7:return "左";
            case 8:return "右";
            case 9:return "右";
            case 10:return "左";
            case 11:return "左";
            case 12:return "右";
            case 13:return "右";
            case 14:return "左";
            case 15:return "右";
            case 16:return "左";
            case 17:return "左";
            case 18:return "右";
            case 19:return "左";
            case 20:return "右";
            case 21:return "左";
            case 22:return "左";
            default:return null;
        }
    }
}
